<?php
require_once("includes/include.php");
class blogcomment extends daobase {
	public function setBlogComment($blogid,$usernumber,$username,$parantid,$replyto,$content,$date){
		$sql = "insert into blogcomment(blogid,parentid,usernumber,username,replyto,postdate,content) 
		values('$blogid','$parantid','$usernumber','$username','$replyto','$date','$content')";
		$query = $this->queryit($sql);
		return $query;
	}
	public function getUserInfo($usernumber){
		$sql = "select user_name,image from user where user_number='$usernumber'";
		$userinfo = $this->fetchit($sql);
		return $userinfo[0];
	}
	public function gogetcommentblogowner($blogid){
		$sql = "select userId from blog where id ='$blogId'";
		$userid = $this->fetchit($sql);
		$userid =$userid[0][0];
		$sql = "select user_number from user where id ='$userid'";
		$usernumber = $this->fetchit($sql);
		return $usernumber[0][0];
	}
	public function getBlogidByContent($content,$date,$usernumber){
		$sql = "select id from blogcomment where content ='$content' and postdate = '$date' and usernumber='$usernumber'";
		$id = $this->fetchit($sql);
		return $id[0][0];
	}
}
$post = $_POST;
if (!empty($post)){
	$commentsubmit = new blogcomment();
	$blogid = $post["blogid"];
	$usernumber = $post["usernumber"];
	$username = $post["username"];
	$parantid = $post["parantid"];
	$replyto = $post["replyto"];
	$content = $post["content"];
	date_default_timezone_set("Asia/shanghai");
	$date = date("Y-m-d H:i:s");
	$addComment = $commentsubmit->setBlogComment($blogid,$usernumber,$username,$parantid,$replyto,$content,$date);
	if ($addComment){
		echo "评论成功";
		$commentblogowner=$commentsubmit->gogetcommentblogowner($blogid);
		$blogidhandle = $commentsubmit->getBlogidByContent($content,$date,$usernumber);
		$userInfo = $commentsubmit->getUserInfo($usernumber);
		if ($replyto){
			$replyto = $commentsubmit->getUserInfo($replyto);
			$replyto=$replyto[0];
		}
		//var_dump($userInfo);
		if (!$userInfo['image']){
			 $userInfo['image'] = "config/images/userconfighead.jpg";
		}
		?>
		<div class="blogcommentsdisplayone">
		<div class="blogcommentsdisplayleft">
			<img src="<?php echo $userInfo['image']?>" alt="userhead" width="80px" height="100px" /><br />
			<?php echo $userInfo['user_name']?>
		</div>
		<div class="blogcommentsdisplayright">
			<?php if($replyto)echo "回复".$replyto; ?>
			<?php echo $date;?><br />
			<?php echo $content;?>
			<?php @session_start();$sessionuser = $_SESSION["userNumber"]; ?>
			<?php if ($usernumber==$sessionuser||$sessionuser==$commentblogowner){echo "<br /><input type='button'  value='删除' id='blogdeletebutton_$blogidhandle' onclick=deleteblogcomment($blogidhandle) />";}?>
		</div>
	</div>
	<?php
	}else {
		echo "评论失败";
	}
}



?>